package com.yunhe.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.yunhe.bean.UserTier;
import com.yunhe.inter.LogDao;
import com.yunhe.inter.UserDao;
import com.yunhe.util.SqlSessionFactoryUtil;

/**
 * Servlet implementation class LogSerlvet
 */
@WebServlet("/LogSerlvet")
public class LogSerlvet extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 1L;
	SqlSession sqlSession; 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LogSerlvet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        
		String userName=request.getParameter("userName");
		String passWord=request.getParameter("passWord");
		sqlSession =  SqlSessionFactoryUtil.openSqlSession();
		LogDao mapper=sqlSession.getMapper(LogDao.class);
		UserTier ut=mapper.Login(userName, passWord);
		System.out.println(ut.getAccountavailable());
		
		if(ut==null ||"".equals(null)){
		 request.getRequestDispatcher("login.jsp").forward(request, response);
        }else if(ut.getAccountavailable()==0){
         request.getSession().setAttribute("UserRole", ut.getUserRole());
         request.getSession().setAttribute("username", ut.getUserName());
         request.getRequestDispatcher("index.jsp").forward(request, response);
        }else{
         request.getRequestDispatcher("login.jsp").forward(request, response);
        }
		

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
